package com.hu.web.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.hu.common.annotation.Log;
import com.hu.common.core.controller.BaseController;
import com.hu.common.core.domain.AjaxResult;
import com.hu.common.enums.BusinessType;
import com.hu.system.domain.ProfitReturn;
import com.hu.system.service.IProfitReturnService;
import com.hu.common.utils.poi.ExcelUtil;
import com.hu.common.core.page.TableDataInfo;

/**
 * 回款明细Controller
 *
 * @author hu
 * @date 2025-01-21
 */
@RestController
@RequestMapping("/business/profit/return")
public class ProfitReturnController extends BaseController {
    @Autowired
    private IProfitReturnService profitReturnService;

    /**
     * 查询回款明细列表
     */
    @PreAuthorize("@ss.hasPermi('profit:return:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProfitReturn profitReturn) {
        startPage();
        List<ProfitReturn> list = profitReturnService.selectProfitReturnList(profitReturn);
        return getDataTable(list);
    }

    /**
     * 获取回款明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('profit:return:query')")
    @GetMapping(value = "/{returnId}")
    public AjaxResult getInfo(@PathVariable("returnId") Long returnId) {
        ProfitReturn profitReturn = profitReturnService.selectProfitReturnByReturnId(returnId);
        if (profitReturn == null) {
            return error("您没有权限查看该回款记录或记录不存在");
        }
        return success(profitReturn);
    }

    /**
     * 新增回款明细
     */
    @PreAuthorize("@ss.hasPermi('profit:return:add')")
    @Log(title = "回款明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProfitReturn profitReturn) {
        return toAjax(profitReturnService.insertProfitReturn(profitReturn));
    }

    /**
     * 修改回款明细
     */
    @PreAuthorize("@ss.hasPermi('profit:return:edit')")
    @Log(title = "回款明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProfitReturn profitReturn) {
        return toAjax(profitReturnService.updateProfitReturn(profitReturn));
    }

    /**
     * 删除回款明细
     */
    @PreAuthorize("@ss.hasPermi('profit:return:remove')")
    @Log(title = "回款明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{returnIds}")
    public AjaxResult remove(@PathVariable Long[] returnIds) {
        return toAjax(profitReturnService.deleteProfitReturnByReturnIds(returnIds));
    }

    /**
     * 导入回款明细
     */
    @PreAuthorize("@ss.hasPermi('profit:return:import')")
    @Log(title = "回款明细", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, Long shopId) throws Exception {
        // 校验店铺ID
        if (shopId == null) {
            return error("请选择所属店铺");
        }
        
        ExcelUtil<ProfitReturn> util = new ExcelUtil<ProfitReturn>(ProfitReturn.class);
        // 指定读取"结算-交易收入"这个sheet页面，标题在第1行（titleNum=0表示第一行是标题）
        List<ProfitReturn> profitReturnList = util.importExcel("交易结算", file.getInputStream(), 0);

        String message = profitReturnService.importProfitReturn(profitReturnList, updateSupport, shopId);
        return success(message);
    }

    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ProfitReturn> util = new ExcelUtil<ProfitReturn>(ProfitReturn.class);
        util.importTemplateExcel(response, "回款明细数据");
    }
}

